package org.jeecg.modules.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.inventory.entity.*;
import org.jeecg.modules.inventory.mapper.BussInventoryDetailsMapper;
import org.jeecg.modules.inventory.mapper.BussInventoryInoutDetailsMapper;
import org.jeecg.modules.inventory.mapper.BussInventoryLedgerMapper;
import org.jeecg.modules.inventory.mapper.BussInventoryMapper;
import org.jeecg.modules.inventory.service.IBussInventoryLedgerService;
import org.jeecg.modules.inventory.service.IBussInventoryService;
import org.jeecg.modules.inventory.vo.BussInventoryPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.*;

/**
 * @Description: 库存台账
 * @Author: jeecg-boot
 * @Date:   2024-07-01
 * @Version: V1.0
 */
@Service
public class BussInventoryLedgerServiceImpl implements IBussInventoryLedgerService {
	@Autowired
	private BussInventoryLedgerMapper bussInventoryLedgerMapper;

	@Override
	public IPage<BussInventoryLedger> myPage(Page<BussInventoryLedger> page, BussInventoryLedger bussInventoryLedger, Map<String, String[]> parameterMap) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String startTime = "";
		String endTime = "";
		String warehouseId = "";
		String categoryCode = "";
		try {
			if (parameterMap.containsKey("warehouseId")) {
				warehouseId = parameterMap.get("warehouseId")[0].toString();
			}
			if (parameterMap.containsKey("categoryCode")) {
				categoryCode = parameterMap.get("categoryCode")[0].toString();
			}
			String type = parameterMap.get("type")[0].toString();
			if (type.equals("0")) {
				String endDay = parameterMap.get("endDay")[0].toString();
				//按日期查询
				Date day = dateFormat.parse(endDay);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(day);
				calendar.add(Calendar.DAY_OF_MONTH, -1); //当前时间减去一天，即一天前的时间
				startTime = dateFormat.format(calendar.getTime());
				endTime = endDay;

				System.out.println(startTime);
				System.out.println(endTime);
			}else if (type.equals("1")){
				//按月份查询
				String month = parameterMap.get("yearmonth")[0].toString();
				LocalDate date = LocalDate.parse(month+"-01");
				YearMonth previousMonth = getPreviousMonth(date);
				startTime = previousMonth + "-28";
				endTime = month + "-28";
				System.out.println(startTime);
				System.out.println(endTime);
			}else {
				startTime = parameterMap.get("beginTime")[0].toString();
				Date day = dateFormat.parse(startTime);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(day);
				calendar.add(Calendar.DAY_OF_MONTH, -1); //当前时间减去一天，即一天前的时间
				startTime = dateFormat.format(calendar.getTime());
				endTime = parameterMap.get("endTime")[0].toString();
				System.out.println(startTime);
				System.out.println(endTime);
			}
			return bussInventoryLedgerMapper.myPage(page,startTime,endTime,categoryCode,warehouseId);
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<BussInventoryLedgerDetails> selectByMainId(String warehouseId, String materialId, String yearmonth) {
		LocalDate date = LocalDate.parse(yearmonth+"-01");
		YearMonth previousMonth = getPreviousMonth(date);
		String startTime = previousMonth + "-28";
		String endTime = yearmonth + "-28";
		//查询期初余额
		BussInventoryLedgerDetails ledgerDetail = bussInventoryLedgerMapper.findOpeningBalance(warehouseId,materialId,startTime);
		List<BussInventoryLedgerDetails> inoutDetail = bussInventoryLedgerMapper.findInoutDetails(warehouseId,materialId,startTime,endTime);
		List<BussInventoryLedgerDetails> resList = new ArrayList<BussInventoryLedgerDetails>();
		resList.add(ledgerDetail);
		BigDecimal quantity = ledgerDetail.getQuantity();
		BigDecimal inventoryQuantity = new BigDecimal(0);
		BigDecimal outboundQuantity = new BigDecimal(0);
		BigDecimal inQuantity = new BigDecimal(0);
		BigDecimal outQuantity = new BigDecimal(0);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		if (inoutDetail !=null && inoutDetail.size()>0) {
			String firstTime = dateFormat.format(inoutDetail.get(0).getInOutDate());
			for (BussInventoryLedgerDetails details : inoutDetail) {
				String time = dateFormat.format(details.getInOutDate());
				if (firstTime.equals(time)){
					if (details.getInventoryQuantity() != null && details.getInventoryQuantity().compareTo(BigDecimal.ZERO) != 0) {
						inventoryQuantity = inventoryQuantity.add(details.getInventoryQuantity());
						inQuantity = inQuantity.add(details.getInventoryQuantity());
						quantity = quantity.add(details.getInventoryQuantity());
						details.setQuantity(quantity);
					}
					if (details.getOutboundQuantity() != null && details.getOutboundQuantity().compareTo(BigDecimal.ZERO) != 0) {
						outboundQuantity = outboundQuantity.add(details.getOutboundQuantity());
						outQuantity = outQuantity.add(details.getOutboundQuantity());
						quantity = quantity.subtract(details.getOutboundQuantity());
						details.setQuantity(quantity);
					}
					resList.add(details);
				}else {
					firstTime = time;
					BussInventoryLedgerDetails dayTotal = new BussInventoryLedgerDetails();
					dayTotal.setDigest("合计");
					dayTotal.setInventoryQuantity(inventoryQuantity);
					dayTotal.setOutboundQuantity(outboundQuantity);
					resList.add(dayTotal);
					inventoryQuantity = new BigDecimal(0);
					outboundQuantity = new BigDecimal(0);
					if (details.getInventoryQuantity() != null && details.getInventoryQuantity().compareTo(BigDecimal.ZERO) != 0) {
						inventoryQuantity = inventoryQuantity.add(details.getInventoryQuantity());
						inQuantity = inQuantity.add(details.getInventoryQuantity());
						quantity = quantity.add(details.getInventoryQuantity());
						details.setQuantity(quantity);
					}
					if (details.getOutboundQuantity() != null && details.getOutboundQuantity().compareTo(BigDecimal.ZERO) != 0) {
						outboundQuantity = outboundQuantity.add(details.getOutboundQuantity());
						outQuantity = outQuantity.add(details.getOutboundQuantity());
						quantity = quantity.subtract(details.getOutboundQuantity());
						details.setQuantity(quantity);
					}
					resList.add(details);
				}
			}
			BussInventoryLedgerDetails dayTotal = new BussInventoryLedgerDetails();
			dayTotal.setDigest("合计");
			dayTotal.setInventoryQuantity(inventoryQuantity);
			dayTotal.setOutboundQuantity(outboundQuantity);
			resList.add(dayTotal);
			BussInventoryLedgerDetails allTotal = new BussInventoryLedgerDetails();
			allTotal.setDigest("合计");
			allTotal.setInventoryQuantity(inQuantity);
			allTotal.setOutboundQuantity(outQuantity);
			resList.add(allTotal);
		}
		return resList;
	}

	public static YearMonth getPreviousMonth(LocalDate date) {
		return YearMonth.from(date).minusMonths(1);
	}
}
